package fcy.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.view.View;
import com.xxmassdeveloper.mpchartexample.R;
import fcy.util.DensityUtil;

/**
 * Created by Fcy on 2016/5/24.<br/>
 * 网格线
 */
public class GridLine extends View {

    private Paint paint;
    /**
     * 水平方向第一条线的偏移量
     */
    private float xOffset;
    /**
     * 竖直方向第一条线的偏移量
     */
    private float yOffset;
    private float mGridSpace;
    private boolean mIsDashesLine = true;
    // resId
    private int mLineColor;
    private float mLineWidth;
    /**
     * 虚线分割长度
     */
    private float mLimit;

    //public GridLine(Context context) {
    //    super(context);
    //    initPaint();
    //}

    public GridLine(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.GridLine);
        xOffset = typedArray.getDimension(R.styleable.GridLine_x_offset, 0);
        mGridSpace = typedArray.getDimension(R.styleable.GridLine_grid_space, 30);
        yOffset = typedArray.getDimensionPixelSize(R.styleable.GridLine_y_offset, 0);
        mLimit = typedArray.getDimensionPixelSize(R.styleable.GridLine_dashes_limit, 3);
        mLineWidth = typedArray.getDimensionPixelSize(R.styleable.GridLine_line_width, 0);
        if (mLineWidth == 0) {
            mLineWidth = DensityUtil.dip2px(context, 0.3f);
        }
        mIsDashesLine = typedArray.getBoolean(R.styleable.GridLine_dashesLine, true);
        mLineColor = typedArray.getResourceId(R.styleable.GridLine_line_color, -2);
        if (mLineColor == -2) {
            mLineColor = R.color.light_gray;
        }
        initPaint();
    }

    private void initPaint() {
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        //paint.setColor(mLineColor);
        //paint.setStrokeWidth(mLineWidth);
        paint.setAntiAlias(true);
    }

    ///**
    // * 虚线还是实线
    // */
    //public void isDashesLine(boolean isDashesLine) {
    //    mIsDashesLine = isDashesLine;
    //}
    //
    //public void setGridLine(int lineColor, float lineWidth) {
    //    mLineColor = lineColor;
    //    mLineWidth = lineWidth;
    //}
    //
    //public void setLimit(float limit) {
    //    mLimit = limit;
    //}
    //
    ///**
    // * @param xMargin
    // *         竖直网格线左右的margin 单位dp
    // * @param yMargin
    // *         必须要设置一个topbuttom Margin 否则贴到顶部和底部，就看不清了 单位dp
    // * @param space
    // *         网格之间的距离（xy相等）单位dp
    // */
    //public void setMargins(float xMargin, float yMargin, float space) {
    //    this.xOffset = Utils.convertPixelsToDp(xMargin);
    //    this.yOffset = Utils.convertPixelsToDp(yMargin);
    //    this.mGridSpace = Utils.convertPixelsToDp(space);
    //}

    protected void onDraw(Canvas canvas) {
        paint.setColor(getResources().getColor(mLineColor));
        paint.setStrokeWidth(mLineWidth);

        if (mIsDashesLine) {
            drawDashesGrid(canvas);
        } else {
            drawSolidLine(canvas);
        }
    }

    private void drawDashesGrid(Canvas canvas) {
        Path path = new Path();
        // 网格左右两边间距
        PathEffect effects = new DashPathEffect(new float[] { mLimit, mLimit, mLimit, mLimit }, 1);
        for (int i = 0; i < getHeight() / mGridSpace; i++) {
            // 水平起点，不要写0，会有一部分被遮住
            path.moveTo(mLineWidth, yOffset + i * mGridSpace);
            // 终点
            path.lineTo(getWidth(), yOffset + i * mGridSpace);
            paint.setPathEffect(effects);
            canvas.drawPath(path, paint);
            // 每次绘制完必须reset
            path.reset();
        }

        for (int i = 0; i < getWidth() / mGridSpace; i++) {
            // 保证最后一条线不超过水平线的终点位置
            float xFrom = xOffset + i * mGridSpace;
            if (xFrom < getWidth()) {
                path.moveTo(xFrom, yOffset);
                path.lineTo(
                        xOffset + i * mGridSpace,
                        yOffset + ((int) (getHeight() / mGridSpace)) * mGridSpace);
                paint.setPathEffect(effects);
                canvas.drawPath(path, paint);
                path.reset();
            }
        }
    }

    /**
     * TODO 画实线
     *
     * @param canvas
     */
    private void drawSolidLine(Canvas canvas) {

    }
}
